package cn.kgc.vue.controller;

import cn.kgc.vue.aop.AopLog;
import cn.kgc.vue.aop.Opration;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.form.CourseLsitForm;
import cn.kgc.vue.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 *@title CourseController
 *@author 韩旭
 *@version 1.0.0
 *@create 2023/8/19 14:02
 **/
@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("list")
    public ResponseResult list() {
        ResponseResult responseResult = new ResponseResult();
        List<Course> list = courseService.list();
        responseResult.success(ResponseStatus.SUCCESS, list);
        return responseResult;
    }

    //模糊查询和分页查询
    @GetMapping("getlist")

    public ResponseResult getlist(CourseLsitForm courseLsitForm){
        return  courseService.getlist(courseLsitForm);
    }
    //增加和修改操作
    @PostMapping("addOrupdate")
    public ResponseResult addOrupdate(@RequestBody Course course){
        ResponseResult responseResult = new ResponseResult();
        Integer id = course.getId();
        if(ObjectUtils.isNotNull(id)){
            //修改操作
            courseService.saveOrUpdate(course);
            responseResult.success(ResponseStatus.UPDATE_SUCCESS);
            return responseResult;
        }
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Course::getCourseName,course.getCourseName())
                .or()
                .eq(Course::getCourseNo,course.getCourseNo());
        Course one = courseService.getOne(wrapper);
        if(ObjectUtils.isNotEmpty(one)){
            responseResult.fail(ResponseStatus.COURSE_EXIT);
            return responseResult;
        }
        courseService.saveOrUpdate(course);
        responseResult.success(ResponseStatus.ADD_SUCCESS);
        return responseResult;
    }

    //删除
    @DeleteMapping("delete/{id}")
    public ResponseResult delete(@PathVariable("id") String id){
        return courseService.delete(id);
    }

}
